<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow=true" type="primary" plain round>新增</el-button>
    </div>
    <el-table :data="seckillList" style="width: 100%">
      <el-table-column prop="id" label="编号" min-width="180">
      </el-table-column>
      <el-table-column prop="title" label="活动名称" min-width="180">
      </el-table-column>
      <el-table-column prop="begintime" label="开始时间" min-width="180">
          <template v-slot="props">
              {{props.row.begintime | dateFmt}}
          </template>
      </el-table-column>
      <el-table-column prop="endtime" label="结束时间" min-width="180">
          <template v-slot="props">
              {{props.row.endtime | dateFmt('/')}}
          </template>
      </el-table-column>
      <el-table-column prop="status" label="活动状态" min-width="180">
          <template v-slot="$props">
              <el-button type="primary" size="mini" round plain v-if="$props.row.status===1" @click="handle($props)">正常</el-button>
              <el-button type="success" size="mini" round plain v-else>禁用</el-button>
          </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button @click="edit(props.row)" type="primary" plain round size="mini">编辑</el-button>
         <el-button @click="del(props.row.id)" type="warning" plain round size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info"/>
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from './Dialog';
// 导入moment
import moment from "moment";
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  data(){
    return {
      info:{
        // 控制对话框组件显示状态
        isShow:false
      }
    }
  },
  components:{Dialog},
  filters: {
    // 时间格式化
    // timestamp: 系统自动注入的待处理的数据
    // divider: 年月日分隔符, 需要手动传递
    dateFmt(timestamp,divider='-') {
        // 数据接口返回的开始时间和结束时间是字符串类型, 一定要将其转换成数值类型
        return moment(parseInt(timestamp)).format(`YYYY${divider}MM${divider}DD hh:mm:ss`);
    },
  },
  computed: {
    ...mapState("seckill", ["seckillList"]),
  },
  methods: {
    ...mapActions("seckill", ["getSeckillListAction"]),
    handle(params){
        console.log(params);
    },
    // 删除
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/seckdelete',{id}).then(res=>{
          if(res.code===200){
            this.$message.success(res.msg);
            // 更新数据列表
            this.getSeckillListAction();
          }else{
            this.$message.error(res.msg);
          }
        });
      });
    },
     // 编辑按钮点击事件处理函数
    edit(obj){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的活动信息传递给对话框组件做默认展示
      this.$refs.dialog.setSeckillData(obj);
    }
  },
  created() {
    // 获取秒杀活动列表
    this.getSeckillListAction();
  },
};
</script>

<style>
</style>